package com.dnapipeline.modelManage.service;

import com.dnapipeline.modelManage.entity.PipelineModel;
import com.dnapipeline.modelManage.repository.PipelineRepository;
import com.dnapipeline.modelManage.repository.ProfileRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PipelineService {

    @Autowired
    private PipelineRepository pipelineRepository;

    @Autowired
    private ProfileRepository profileRepository;

    public List<PipelineModel> findAll() {
        return pipelineRepository.findAll();
    }

    public PipelineModel create(PipelineModel pipeline) {
        // Key在PipelineModel的无参构造方法中自动生成
        return pipelineRepository.save(pipeline);
    }

    public List<PipelineModel> findByPipelineKey(String pipelineKey) {
        return pipelineRepository.findByPipelineKey(pipelineKey);
    }

    public PipelineModel update(String pipelineKey, PipelineModel pipelineModel) {
        List<PipelineModel> optionalPipeline = pipelineRepository.findByPipelineKey(pipelineKey);
        if(optionalPipeline.size() > 0){
            PipelineModel pipeline = optionalPipeline.get(0);
            pipeline.setPipelineName(pipelineModel.getPipelineName());
            pipeline.setPipelineContent(pipelineModel.getPipelineContent());
            pipeline.setIsOnline(pipelineModel.getIsOnline());
            return pipelineRepository.save(pipeline);
        }
        return null;
    }

    @Transactional
    public void delete(String pipelineKey) {
        // 删除pipeline
        pipelineRepository.deleteById(pipelineKey);
    }

}
